package com.ludomancy.playground;


import java.awt.Graphics;
import java.awt.Rectangle;

import com.ludomancy.gamelib.Accumulator;
import com.ludomancy.gamelib.Randomizer;
import com.ludomancy.gamelib.Widget;
import com.ludomancy.gamelib.entity.WorldWidget;
import com.ludomancy.gamelib.ui.Button;
import com.ludomancy.gamelib.ui.Button.ButtonListener;

public class PlaygroundWorldWidget extends WorldWidget implements ButtonListener
{
	private Accumulator shake;
	public Button reset;

	public PlaygroundWorldWidget(Widget parent, Rectangle area)
	{
		super(parent, area, Game.game.world);
		reset = new Button(this, new Rectangle(0,area.height-21, area.width, 20), "Reboot", this);
	}
	
	public void onPress(Button widget)
	{
		if(widget == reset)
		{
			PlaygroundApp.app.resetGame();
		}
	}

	public void startShake()
	{
		shake = new Accumulator(450);
	}
		
	public void update()
	{
		if(Game.game != null)
		{
			Game.game.update();
		}
	}
		
	public void paint(Graphics g)
	{
		int shakeAmount = 0;
		if(shake != null && !shake.isDone())
		{
			shakeAmount = Randomizer.getRandom(-3,3);
			g.translate(0, shakeAmount);
		}
		Game.game.terrain.paint(g);
		super.paint(g);
		if(shake != null && !shake.isDone())
		{
			g.translate(0, -shakeAmount);
			shake.update();
		}
	}	
}
